Prisma middleware are functions registered with $use() that intercept every query before and after execution. They receive params (model, action, args) and a next function. Use them for audit logging on write operations, global soft-delete filters, query performance monitoring, and cross-cutting data transformations.
Middleware is registered with $use() before $connect() — order of registration determines execution order.
params.model is the model name (e.g. 'User'); params.action is the operation (e.g. 'create', 'findMany').
Always call and return await next(params) — omitting it prevents the query from executing.
Middleware runs for every query including those inside $transaction() callbacks.
Prisma 5+ deprecates $use() in favor of Prisma Client Extensions — plan migrations accordingly.